package com.csii.mobilebanking.expandablelistview.fragment;

import com.csii.mobilebanking.R;
import com.csii.mobilebanking.expandablelistview.util.Constant;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.RadioGroup.OnCheckedChangeListener;

@SuppressLint("Recycle")
public abstract class BaseFragment extends Fragment implements
		OnCheckedChangeListener {
	
	private static final String MAMIN_TAG = "BaseFragment";

	protected TabHost tabHost;

	/***
	 * 创建RadioButton
	 * 
	 * @param title
	 * @param tag
	 * @return 添加RadioButton
	 */
	protected void createRadioButton(ViewGroup view, String title, int id) {
		// LayoutInflater inflater=LayoutInflater.from(getActivity());
		@SuppressWarnings("static-access")
		RadioGroup v = (RadioGroup) view.inflate(getActivity(),
				R.layout.tab_title_rbtn, view);
		int count = v.getChildCount();
		
		// 拿取最后一个 getTouchables 是拿取可触摸的View
		RadioButton radioButton = (RadioButton) v.getTouchables()
				.get(count - 1);
		
		if (radioButton != null) {
			Log.d(MAMIN_TAG, "radioButton id.." + id);
			radioButton.setId(id);
			radioButton.setRight(60);
			radioButton.setText(title);
		}

	}

	protected void initView(LayoutInflater inflater, int titlesId, int tagsId,
			int idsId,int layoutsId, int contentViewsId) {
		tabHost = (TabHost) inflater.inflate(R.layout.tahost, null);
		tabHost.setup();
		// 单选按钮组
		RadioGroup radioGroup = (RadioGroup) tabHost
				.findViewById(R.id.rg_title);
		// 设置监听事件
		radioGroup.setOnCheckedChangeListener(this);

		// 获取数据
		String[] titles = getResources().getStringArray(titlesId);
		String[] tags = getResources().getStringArray(tagsId);

		int length = titles.length;
		// int[] ids=getResources().getIntArray(R.array.rbtn_ids_cim);
		TypedArray ids = getResources().obtainTypedArray(idsId);
		
		TypedArray layouts=getResources().obtainTypedArray(
				layoutsId);
		
		TypedArray contentViews = getResources().obtainTypedArray(
				contentViewsId);
		// titles tags ids contentVIews 长度要一致
		if (length != tags.length || length != ids.length()
				|| contentViews.length() != length) {
			throw new RuntimeException(
					"titles tags  ids  contentViews   Length is not consistent！");
		}
		
		
		for (int i = 0; i < length; i++) {
			// create RadioButton
			createTab(layouts.getResourceId(i, 0),contentViews.getResourceId(i, 0), tags[i]);
			createRadioButton(radioGroup, titles[i], ids.getResourceId(i, 0));		

		}
		((RadioButton)radioGroup.getTouchables().get(0)).setChecked(true);
		//释放资源
		ids.recycle();
		contentViews.recycle();
		layouts.recycle();

	}

	@SuppressWarnings("static-access")
	protected void createTab(int layoutId,int viewId, String tag) {
		
		tabHost.inflate(getActivity(), layoutId, tabHost.getTabContentView());
		tabHost.addTab(tabHost.newTabSpec(tag).setContent(viewId)
				.setIndicator(tag));
	}

	public int getShownIndex() {
		return getArguments().getInt(Constant.FRAGMENT_INDEX, 0);
	}

	public static BaseFragment newInstance(int index) {
		BaseFragment baseFragment=null;
		
		//参数
		Bundle args = new Bundle();
		args.putInt(Constant.FRAGMENT_INDEX, index);
		
		Log.d(MAMIN_TAG, "newInstance..初始化Fragment.." + index);
		switch (index) {
			case 0:
				baseFragment=new CustomerIMFragment();
				break;
			case 1:
				baseFragment=new CustomerFPFragment();
				break;
			case 2:
				baseFragment=new FinancialPMFragment();
				break;
			default:
				baseFragment=new CustomerIMFragment();
				break;
			}
		baseFragment.setArguments(args);
		return baseFragment;
	}
}
